package com.tdkj.action.backstage.shop;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;

import com.tdkj.action.BaseAction;
import com.tdkj.model.shop.BeanGood;
import com.tdkj.service.shop.IBeanGoodService;
import com.tdkj.util.typeconverter.DateTypeConverter;


public class GoodManageAction extends BaseAction {
    
	private IBeanGoodService goodService;
	
	private String auditState;
	
	private Long id;
	
	private List<BeanGood> goods ;
	
	public String execute() {
		String keyword = this.getQueryInfo().get("keyword");
		String column = this.getQueryInfo().get("column");
		Date publishDateStart = DateTypeConverter.convertToDate(this.getQueryInfo().get("publishDateStart")==null?"":this.getQueryInfo().get("publishDateStart"));
		Date publishDateEnd = DateTypeConverter.convertToDate(this.getQueryInfo().get("publishDateEnd")==null?"":this.getQueryInfo().get("publishDateEnd"));
		String priceStart = this.getQueryInfo().get("priceStart");
		String priceEnd = this.getQueryInfo().get("priceEnd");
		
		
		String hql = "from " + BeanGood.class.getName() + " as bean where 1=1 ";
		if(keyword!=null && column!=null && !"".equals(keyword) && !"".equals(column)){
			hql = hql + " and bean."+column+" like '%"+keyword+"%' ";
		}
		if(publishDateStart!=null){
			hql = hql + " and bean.publishTime>='"+DateTypeConverter.convertToString(publishDateStart,"yyyy-MM-dd")+"' ";
		}
		if(publishDateEnd!=null){
			hql = hql + " and bean.publishTime<='"+DateTypeConverter.convertToString(publishDateEnd,"yyyy-MM-dd")+"' ";
		}
		if(priceStart!=null && !"".equals(priceStart)){
			hql = hql + " and bean.price>='"+priceStart+"' ";
		}
		if(priceEnd!=null && !"".equals(priceEnd)){
			hql = hql + " and bean.price<='"+priceEnd+"' ";
		}
		hql = hql + " order by bean.publishTime desc";
		Query query = this.createQuery(hql);
		this.queryByPage(query, "goodManageAction.action", 10);
		
		return "success";
	}
	
	public String audit() {
		goodService.changeAudit(id, auditState);
		return execute();
	}

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the goods
	 */
	public List<BeanGood> getGoods() {
		return goods;
	}

	/**
	 * @param goods the goods to set
	 */
	public void setGoods(List<BeanGood> goods) {
		this.goods = goods;
	}

	/**
	 * @param goodService the goodService to set
	 */
	@Resource
	public void setGoodService(IBeanGoodService goodService) {
		this.goodService = goodService;
	}

	/**
	 * @return the auditState
	 */
	public String getAuditState() {
		return auditState;
	}

	/**
	 * @param auditState the auditState to set
	 */
	public void setAuditState(String auditState) {
		this.auditState = auditState;
	}


}
